package com.auemall.controller;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.auemall.constant.ApplicationConstant;
import com.auemall.service.TicketService;
import com.auemall.utils.EUDataGridResult;
import com.auemall.vo.BaseRespVO;
import com.auemall.vo.TicketReqVO;
import com.auemall.vo.TicketVO;


/**
 * 开票管理Controller
 * <p>Title: TicketController</p>
 * <p>Description: </p>
 *
 */
@Controller
public class TicketController {
	
	private Logger logger = Logger.getLogger(TicketController.class);
	
	@Autowired
	private TicketService ticketService;
	
	/**
	 * 查看所有
	 * @param req
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/ticket/v1/list")
    @ResponseBody
	public EUDataGridResult selectTicketList(@RequestBody TicketReqVO req,Integer page, Integer rows){
		EUDataGridResult result = null;
		
		try {
			result = ticketService.selectTicketList(req, page, rows);
		} catch (Exception e) {
			result = new EUDataGridResult();
	        result.setResult(ApplicationConstant.RESULT_FALTURE);
	        result.setMsg("ticket/v1/list 查询开票信息失败："+e.getMessage());
	        logger.error(result.getMsg());
		}	
		return result;
	}
	
	
	/**
	 * 查看某一开票资料
	 * @param req
	 * @param flag
	 * @return
	 */
	@RequestMapping("/ticket/v1/showTicket")
    @ResponseBody
	public TicketVO showTicket(TicketVO req){
		TicketVO result = null;
		
		try {
			result = ticketService.selectTicketById(req.getId());
		} catch (Exception e) {
			
		}	
		return result;
	}
	
	/**
	 * 审核开票资料
	 * @param req
	 * @param flag
	 * @return
	 */
	@RequestMapping("/ticket/v1/checkTicket")
    @ResponseBody
	public BaseRespVO checkTicket(TicketVO req,Integer flag){
		BaseRespVO result = null;
		
		try {
			result = ticketService.updateTicketInfo(req,flag);
		} catch (Exception e) {
			result = new EUDataGridResult();
	        result.setResult(ApplicationConstant.RESULT_FALTURE);
	        result.setMsg("ticket/v1/checkTicket 审核开票资料失败："+e.getMessage());
	        logger.error(result.getMsg());
		}
		return result;
	}
	
	/**
	 * 删除开票资料
	 * @param req
	 * @param flag
	 * @return
	 */
	@RequestMapping("/ticket/v1/deleteTicket")
    @ResponseBody
	public BaseRespVO deleteTicket(@RequestBody String id){
		BaseRespVO result = null;
		try {
			String[] ids = id.split(",");
			result = ticketService.deleteTicketInfo(ids);
		} catch (Exception e) {
			result = new EUDataGridResult();
	        result.setResult(ApplicationConstant.RESULT_FALTURE);
	        result.setMsg("ticket/v1/checkTicket 审核开票资料失败："+e.getMessage());
	        logger.error(result.getMsg());
		}
		return result;
	}
	
	@RequestMapping("/ticket/v1/download")    
    public ResponseEntity<byte[]> download(Integer id) throws IOException {
    	//查出附件地址
		TicketVO result = null;
		try {
			result = ticketService.selectTicketById(id);
			//文件下载
			if(result == null || result.getImageurl() == null){
				logger.error("下载失败-----该文件不存在");
				return null;
			}
			String path=result.getImageurl();  
	        File file=new File(path);  
	        HttpHeaders headers = new HttpHeaders();    
	        String fileName=new String(file.getName().getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题  
	        headers.setContentDispositionFormData("attachment", fileName);   
	        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);   
	        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);  
	        
	        return entity;
		} catch (Exception e) {
			logger.error("下载失败");
			return null;
		}
    	
        
    }    
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
